﻿using UnityEngine;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;

public class BaseCounterType{}
public class CounterManager
{
	private static Dictionary<string,int> countersCache ;
	public static void init(BaseCounterType counterType)
	{
		if(countersCache != null)
		{
			return;
		}
		countersCache = new Dictionary<string, int>();
		ArrayList allCounters = TypeUtils.getAllStaticProperties(counterType);

		for(int count = 0 ; count < allCounters.Count ; count ++)
		{
			string eachCounter = allCounters[count] as string;
			int counterValue = PlayerPrefs.GetInt(eachCounter);
			countersCache.Add(eachCounter,counterValue);
			Debug.Log(eachCounter + "---" + counterValue);
		}
	}

	public static int getCounter(string counterType)
	{
		Debug.Log(counterType);
		Debug.Log(countersCache.Keys);
		return countersCache[counterType];
	}


	public static void incremenetCounter(string counterType ,  int valueToIncrement = 1)
	{
		int value = getCounter(counterType);
		value+=valueToIncrement;
		updateCounterCacheWithPref(counterType,value);
	}


	public static void updateCounter(string counterType ,  int newValue)
	{
		updateCounterCacheWithPref(counterType,newValue);
	}
	private static void updateCounterCacheWithPref(string counterType ,  int finalValue)
	{
		countersCache[counterType] = finalValue;
		PlayerPrefs.SetInt(counterType,finalValue);
	}
}
